package com.zhx.taoda.utils;


import java.io.*;
import java.util.logging.Logger;

/**
 * 文件工具类
 */
public class FileUtils {
    private static final Logger log = Logger.getLogger("FileUtils");
    public static final String ENCODING_GBK = "UTF-8";

    /**
     * 根据文件路径读取文件
     *
     * @param filePath 文件路径
     */
    public static String readFile(String filePath) {
        StringBuilder sb = new StringBuilder();
        try {
            File file = new File(filePath);
            if (file.isFile() && file.exists()) {
                InputStreamReader read = new InputStreamReader(new FileInputStream(file), ENCODING_GBK);
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    sb.append(lineTxt).append("\n");
                }
                read.close();
            } else {
                log.info("未找到指定的文件: " + filePath);
            }
        } catch (Exception e) {
            log.info("读取文件内容出错: " + filePath);
        }
        return sb.toString();
    }

    /**
     * 创建目录
     *
     * @param path 目录地址
     */
    public static void mkdir(String path) {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdir();
        }
    }

    public static void createFile(String path) {
        File file = new File(path);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
